﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDU.DataModel;
using System.Windows.Input;
using DDU.Library;

namespace DDU.ViewModel
{
    public class CourseAddViewModel : MainViewModel
    {
        public CourseAddViewModel()
        {
            SelectedSemester = CurrentSemester;
            scheduleToAdd.ValueChanged -= ScheduleToAdd_ValueChanged;
            scheduleToAdd.ValueChanged += ScheduleToAdd_ValueChanged;
        }

        void ScheduleToAdd_ValueChanged(object sender, PropertyArgs e)
        {
            RaisePropertyChanged(() => ScheduleToAdd);
            RaisePropertyChanged(() => AddCommand);
        }

        private SemesterInfo selectedSemester;
        public SemesterInfo SelectedSemester
        {
            get { return selectedSemester; }
            set
            {
                selectedSemester = value;
                RaisePropertyChanged(() => SelectedSemester);
                RaisePropertyChanged(() => CanAdd);
                ScheduleToAdd.SemesterGuid = SelectedSemester.Guid;
                RaisePropertyChanged(() => ScheduleToAdd);
            }
        }

        public bool CanAdd
        {
            get
            {
                //return true;
                return IsDesignMode ? true : SelectedSemester != null;
            }
        }

        private ScheduleInfo scheduleToAdd = new ScheduleInfo();
        public ScheduleInfo ScheduleToAdd
        {
            get { return scheduleToAdd; }
            set
            {
                scheduleToAdd = value;
                
                RaisePropertyChanged(() => ScheduleToAdd);
                RaisePropertyChanged(() => AddCommand);

                scheduleToAdd.ValueChanged -= ScheduleToAdd_ValueChanged;
                scheduleToAdd.ValueChanged += ScheduleToAdd_ValueChanged;
            }
        }

        private void Insert(ScheduleInfo entity)
        {
            if (string.IsNullOrWhiteSpace(entity.SemesterGuid))
            {
                entity.SemesterGuid = SelectedSemester.Guid;
            }
            Database.CollectionOfSchedules.InsertOnSubmit(entity);
            Submit();
            ScheduleToAdd = new ScheduleInfo();
            RaisePropertyChanged(() => ScheduleToAdd);
            RaisePropertyChanged(() => AddCommand);
        }

        public ICommand AddCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !string.IsNullOrWhiteSpace(ScheduleToAdd.Name) && SelectedSemester!=null && ScheduleToAdd.StartTime != null && ScheduleToAdd.EndTime != null; },
                    ExecuteCallback=delegate{
                        Insert(ScheduleToAdd);
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (BackUri != null)
                        {
                            Navigate(BackUri);
                        }
                    }
                };
            }
        }
    }
}
